/**
 * 
 */
package com.jm.web.action;



import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.utils.Token;
import com.jm.utils.Utils;
import com.jm.web.BaseMainPage;
import com.jm.web.WFPage;
import com.jm.web.data.BaseDataEntity;
import com.jm.web.data.CrudDataEntity;
import com.jm.web.data.JMForm;
import com.jm.web.data.Mail;
import com.jm.web.data.PageResult;
import com.jm.web.data.ReturnData;
import com.jm.web.form.Button;
import com.jm.web.form.Toolbar;
import com.jm.wf.WFHelper;
import com.jm.wf.data.PanTable;
import com.jm.wf.data.WFInstance;
import com.jm.wf.data.WFOpStep;
import com.jm.wf.data.WFStep;
import com.jm.wf.data.WorkFlow;

/**
 * @author yanjie
 *
 * 2011/9/27 下午1:09:44
 */
public class BaseAction {
	protected BaseMainPage page = null ;//頁面對像
	protected Utils utils = null ;//utils
	protected JMForm form = null ;//表單上的數據
	protected ReturnData ret = null ;//後臺執行的結果
	
	//protected PrintWriter out = null;
	
	/**************************流程處理代碼*******************************/
	protected WFHelper wfHelper = null;
	public WFHelper getWFHelper(){
		if(wfHelper == null){
			wfHelper = new WFHelper(this);
		}
		return wfHelper;
	}
	
	/**
	 * 送簽之前
	 */
	public void BeginStartWF()throws Exception{
		
	}
	
	/**
	 * 送簽之后
	 */
	public void EndStartWF()throws Exception{
		
	}
	
	/**
	 * 取消之前
	 */
	public void BeginCancelWF()throws Exception{
		
	}
	
	/**
	 * 取消之后
	 */
	public void EndCancelWF()throws Exception{
		
	}
	
	/**
	 * 簽核之前
	 */
	public void BeginCheckWF()throws Exception{
		
	}
	
	/**
	 * 簽核之后
	 */
	public void EndCheckWF()throws Exception{
		
	}
	/**
	 * @param curStep
	 * @return
	 * 取簽核人
	 */
	public List<BaseDataEntity> getWFRole(WFStep wfstep) throws Exception{
		List<BaseDataEntity> list = null;
		if("hrnum".equalsIgnoreCase(wfstep.getRoleType())){
			list = new ArrayList<BaseDataEntity>();
			String hrnums[] = wfstep.getRole().split(",");
			String chnames[] = wfstep.getRoleRem().split(",");
			String mails[] = wfstep.getRoleMail().split(",");
			for (int i = 0; i < hrnums.length; i++) {
				if(!StringHelper.IsNullOrEmpty(hrnums[i])){
					BaseDataEntity row = new BaseDataEntity();
					row.spsv("hrnum", hrnums[i]);
					row.spsv("chname", chnames[i]);
					row.spsv("mail", mails[i]);
					list.add(row);
				}
			}
			
		}
		return list;
	}
	
	/**
	 * @param curWFInstance
	 * @param curData
	 * @throws Exception
	 * 簽核結果通知送簽人
	 */
	public void sendWFResultMail(WFInstance curWFInstance,CrudDataEntity curData,WFOpStep curWFOpStep) throws Exception{
		WorkFlow wf = getWorkFlow();
		Mail mail = new Mail();
		mail.setEncode(false);
		mail.setContentType(Mail.HTML);
		mail.setTo(curWFInstance.gpsv("sendmail"));
		mail.setSubject(wf.getWFName()+" 單據號:"+getWFBillNo()+" 簽核結果提醒!");
		StringBuffer mailBody = new StringBuffer();
		mailBody.append("單據類型 : "+wf.getWFName()+"<br>");
		mailBody.append("單據號&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: "+getWFBillNo()+"<br>");
		mailBody.append("簽核結果 : "+curWFOpStep.getOpText()+"<br>");
		if(wf.isMailResultWithRecord()){
			mailBody.append("簽核記錄: <br>");
			mailBody.append(curWFInstance.getCheckRecordHtml());
		}
		mail.setBody(mailBody.toString());
		try{
			ProceesWFResultMail(curWFInstance,curData,curWFOpStep,mail);
			if(!StringHelper.IsNullOrEmpty(mail.getTo()))
				utils.sendMail(mail);
		}catch(Exception e){
			try{
				mail.setTo("yanjie@mail.jmknit.com");
				mail.setBody(mail.getBody()+"<br>"+e.getMessage());
				utils.sendMail(mail);
			}catch(Exception e1){
				
			}
		}
	}
	
	/**
	 * @param data
	 * @param mail
	 * @throws Exception
	 * 處理通知制單人及送簽人的郵件
	 */
	protected void ProceesWFResultMail(WFInstance curWFInstance,CrudDataEntity curData,WFOpStep curWFOpStep,Mail mail) throws Exception{
		
	}
	
	/**
	 * @param datas
	 * @throws Exception
	 * 郵件通知簽核人
	 */
	public void SendWFCheckMail(List<BaseDataEntity> datas) throws Exception{
		WorkFlow wf = getWorkFlow();
		for (int i = 0; i < datas.size(); i++) {
			BaseDataEntity data = datas.get(i);
			if(!StringHelper.IsNullOrEmpty(data.gpsv("mail"))){
				Mail mail = new Mail();
				mail.setEncode(false);
				mail.setContentType(Mail.HTML);
				mail.setTo(data.gpsv("mail"));
				mail.setSubject(wf.getWFName()+" 單據號:"+getWFBillNo()+" 簽核提醒!");
				StringBuffer mailBody = new StringBuffer();
				mailBody.append("單據類型 : "+wf.getWFName()+"<br>");
				mailBody.append("單據號&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: "+getWFBillNo()+"<br>");
				mailBody.append("當前步驟 : "+data.gpsv("wfstepname")+"<br>");
				mailBody.append("送簽人&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: "+data.gpsv("sendchname")+"<br>");
				mailBody.append("送簽時間 : "+data.gpsv("sendday")+"<br>");
				String fullpath = getRequest().getRequestURL().toString();
				fullpath = fullpath.substring(0,fullpath.indexOf(getRequest().getContextPath()))+getRequest().getContextPath()+"/";
				fullpath += wf.gpsv("checkpage");
				fullpath += "?wfopstepid="+data.gpsv("wfopstepid");
				mailBody.append("<a href=\""+fullpath+"\" >點擊進入簽核頁面</a>");
				mail.setBody(mailBody.toString());
				try{
					ProceesWFCheckMail(data,mail);
					utils.sendMail(mail);
				}catch(Exception e){
					try{
						mail.setTo("yanjie@mail.jmknit.com");
						mail.setBody(mail.getBody()+"<br>"+e.getMessage());
						utils.sendMail(mail);
					}catch(Exception e1){
						
					}
				}
			}
		}	
	}
	
	/**
	 * @param data
	 * @param mail
	 * @throws Exception
	 * 處理通知簽核人的郵件
	 */
	protected void ProceesWFCheckMail(BaseDataEntity data,Mail mail) throws Exception{
		
	}
	
	/**
	 * @param curWFStep
	 * @return
	 * @throws Exception
	 * 取下一步操作
	 */
	public WFStep getNextStep(WFStep curWFStep) throws Exception{
		if(curWFStep == null){
			return getWorkFlow().getFirstStep();
		}else{
			return getWorkFlow().getNextStep(curWFStep);
		}
	}
	
	/**
	 * @param wf
	 * @return
	 * 返回單據號
	 */
	public String getWFBillNo(){
		PanTable panTable = getWorkFlow().getPanTable();
		return form.gpsv(panTable.getTableName()+panTable.getKeyName());
	}
	
	/**
	 * @return
	 * 返回流程
	 */
	public WorkFlow getWorkFlow(){
		return null;
	}
	
	
	/**
	 * @return
	 * 返回實體數據
	 */
	public CrudDataEntity getDataEntity(){
		PanTable panTable = getWorkFlow().getPanTable();
		CrudDataEntity data = new CrudDataEntity(utils, panTable.getDataSource(), panTable.getDataTable(), panTable.getKeyName());
		data.spsv(data.getKey(), getWFDataKeyValue());
		return data;
	}
	
	/**
	 * @param wf
	 * @return
	 * 取流程实体主键值
	 */
	public String getWFDataKeyValue(){
		PanTable panTable = getWorkFlow().getPanTable();
		return form.gpsv(panTable.getTableName()+panTable.getKeyName());
	}
	
	/**
	 * @param wf
	 * @return
	 * 取流程实体主键名稱
	 */
	public String getWFDataKeyName(){
		PanTable panTable = getWorkFlow().getPanTable();
		return panTable.getTableName()+panTable.getKeyName();
	}
	
	
	/**
	 * @throws Exception
	 * 流程請求處理開始
	 */
	protected void BeginWFProcess() throws Exception{
		WorkFlow wf = getWorkFlow();
		page.setCurWF(wf);
		
		if(page.isWFCheckPage()){//流程簽核模式
			//1.設置當前操作步驟 
			String wfopstepid = form.gpsv("wfopstepid");
			WFOpStep wfOpStep = new WFOpStep(utils, wf.getWFDatasource());
			wfOpStep.setKeyValue(wfopstepid);
			wfOpStep.getSelf();
			page.setCurWFOpStep(wfOpStep);
			
			//2.檢查當前操作人的權限
			boolean bprivilege = wfOpStep.getOpHrnum().equals(utils.getHrNum());
			page.setPrivilege(bprivilege);
			if(!bprivilege)return;
			
			//3.設置當前流程實例
			WFInstance wfinstance = new WFInstance(utils, wf.getWFDatasource());
			wfinstance.setKeyValue(wfOpStep.getWFInstanceID());
			wfinstance.getSelf();
			page.setCurWFInstance(wfinstance);
			
			//4.設置當前簽核步驟 
			String wfstepid = wfinstance.getCurWFStepId();
			WFStep wfStep = wf.getWFStep(wfstepid);
			page.setCurWFStep(wfStep);
			
			//5.設置實體的主鍵及單據號
			form.spsv("wfbillno", wfinstance.getWFBillNo());
			form.spsv("wfdatakeyvalue", wfinstance.getWFdataKeyValue());
			
			//6.設置單據信息
			CrudDataEntity data = getDataEntity();
			data.spsv(data.getKey(), wfinstance.getWFdataKeyValue());
			data.getSelf();
			data.CopyTo(form, false);
			form.spsv(getWFDataKeyName(), wfinstance.getWFdataKeyValue());
			page.setCurData(data);
		}else {
			String wfinstanceid = form.gpsv("wfinstance");
			if(!StringHelper.IsNullOrEmpty(wfinstanceid)){
				WFInstance wfinstance = new WFInstance(utils, wf.getWFDatasource());
				wfinstance.setKeyValue(wfinstanceid);
				wfinstance.getSelf();
				page.setCurWFInstance(wfinstance);
			}
		}
	}
	
	/**
	 * @throws Exception
	 * 流程請求處理結束
	 */
	protected void EndWFProcess() throws Exception{
		
	}
	
	/**
	 * @param wfBtn
	 * 處理送核頁面每一個button 
	 */
	public void ProcessWFStartPageButton(Toolbar toolbar,List defaultItems) throws Exception{
		Button button = null;
		//1.取消簽核按鍵
		button = getButton("check",defaultItems);
		if(button != null) button.setEnab(false);
		
		//2.重定義按鈕的disable
		String wfstate = form.gpsv("wfstate");
		if(form.isNullOrEmpty("wfstate"))return ;
		
		if(WFInstance.WF_STATE_FINSH .equals(wfstate) 
				|| WFInstance.WF_STATE_CHECKING.equals(wfstate)){
			String strbtn = "save,cancel,delete,copy";
			for (int i = 0; i < strbtn.split(",").length; i++) {
				button = getButton(strbtn.split(",")[i],defaultItems);
				if(button != null) button.setDisabled(true);
			}
		}
		
		if(WFInstance.WF_STATE_NOSTART.equals(wfstate)){
			//重寫送簽按鈕
			button = getButton("start",defaultItems);
			if(button != null) {
				button.setOnClick("var _P = {action:'wfstart',actionvalue:'',actiontext:'"+button.getValue()+"'};if(params && params != undefined)$.extend(_P,params);return doAction(_P);");
				button.setEnab(true);
				button.setDisabled(isDisableWFStart());
			}
		}else if(WFInstance.WF_STATE_CHECKING.equals(wfstate)){
			//取消送簽按鈕
			int index = 0;
			button = getButton("start",defaultItems);
			if(button != null) {
				if(isEnabWFCancel()){
					button.setEnab(false);
				}else{
					button.setDisabled(true);
				}
				index = defaultItems.indexOf(button);
			}
			
			if(isEnabWFCancel()){
				//加上取消流程按鈕
				Button wfcancel = toolbar.getNewButton("取消流程", "wfcancel", "wfcancel", "sx-tb-commonaction", toolbar.getBaseTag());
				wfcancel.setOnClick("var _P = {action:'wfcancel',actionvalue:'',actiontext:'"+wfcancel.getValue()+"'};if(params && params != undefined)$.extend(_P,params);return doAction(_P);");
				wfcancel.setDisabled(isDisableWFCancel());
				defaultItems.add(index, wfcancel);
			}
		}else if(WFInstance.WF_STATE_FINSH .equals(wfstate)){
			
		}else if(WFInstance.WF_STATE_BACK .equals(wfstate)){
			//重寫送簽按鈕
			button = getButton("start",defaultItems);
			if(button != null) {
				button.setOnClick("var _P = {action:'wfstart',actionvalue:'',actiontext:'"+button.getValue()+"'};if(params && params != undefined)$.extend(_P,params);return doAction(_P);");
				button.setEnab(true);
				button.setDisabled(isDisableWFStart());
			}
		}else if(WFInstance.WF_STATE_CANCEL .equals(wfstate)){
			//重寫送簽按鈕
			button = getButton("start",defaultItems);
			if(button != null) {
				button.setOnClick("var _P = {action:'wfstart',actionvalue:'',actiontext:'"+button.getValue()+"'};if(params && params != undefined)$.extend(_P,params);return doAction(_P);");
				button.setEnab(true);
				button.setDisabled(isDisableWFStart());
			}
		}
		
		
	}
	
	/**
	 * @return
	 * 流程是否啟用[取消流程]功能
	 */
	protected boolean isEnabWFCancel()throws Exception{
		return getWorkFlow().isEnabWFCancel();
	}
	
	/**
	 * @return
	 * 是否禁用取消流程按鈕
	 */
	protected boolean isDisableWFCancel() throws Exception{
		return false;
	}
	
	/**
	 * @return
	 * @throws Exception
	 * 是否禁用送簽按鈕
	 */
	protected boolean isDisableWFStart() throws Exception{
		return false;
	}
	
	/**
	 * @param wfBtn
	 * 處理簽核頁面每一個button 
	 */
	public void ProcessWFCheckPageButton(Button wfBtn)throws Exception{
		
	}
	
	/**
	 * @param defaultItems
	 * 處理簽核頁面總的button
	 */
	public void ProcessWFCheckPageButtons(List defaultItems)throws Exception{
		
	}
	
	/**
	 * 处理送簽流程
	 */
	protected boolean onWFStart()  throws Exception{
		onWFStartValidate();
		if(ret.isError())return false;
		doWFStart();
		return true ;
	}
	
	/**
	 * 送簽流程验证
	 */
	protected void onWFStartValidate() throws Exception{}
	
	/**
	 * 执行送簽流程
	 */
	protected void doWFStart() throws Exception{
		WFHelper wfHepler =  getWFHelper();
		wfHepler.StartWorkFlow();
	}
	
	/**
	 * 处理流程签核
	 */
	protected boolean onWFCheck()  throws Exception{
		onWFCheckValidate();
		if(ret.isError())return false;
		doWFCheck();
		return true ;
	}
	
	/**
	 * 签核流程验证
	 */
	protected void onWFCheckValidate() throws Exception{}
	
	/**
	 * 执行流程签核
	 */
	protected void doWFCheck() throws Exception{
		String opvalue = form.getActionValue().split("#")[0];
		String optext = form.getActionValue().split("#")[1];
		WFHelper wfHepler =  getWFHelper(); 
		wfHepler.doWFCheck(opvalue,optext);
	}
	
	/**
	 * 处理取消流程
	 */
	protected boolean onWFCancel()  throws Exception{
		onWFCancelValidate();
		if(ret.isError())return false;
		doWFCancel();
		return true ;
	}
	
	/**
	 * 取消流程验证
	 */
	protected void onWFCancelValidate() throws Exception{}
	
	/**
	 * 执行取消流程
	 */
	protected void doWFCancel() throws Exception{
		WFHelper wfHepler =  getWFHelper();
		wfHepler.doWFCancel();
	}
	
	/**
	 * @param name
	 * @param defaultItems
	 * @return
	 */
	protected Button getButton(String name,List defaultItems){
		for (int i = 0; i < defaultItems.size(); i++) {
			if(defaultItems.get(i) instanceof Button){
				if(name.equalsIgnoreCase(((Button)defaultItems.get(i)).getName())){
					return (Button)defaultItems.get(i);
				}
			};
		}
		return null;
	}
	
	/**************************流程處理代碼*******************************/
	
	/**
	 * 
	 */
	public void out(Object obj) {
		System.out.println(String.valueOf(obj));
	}
	
	public void init(BaseMainPage page)throws Exception{
		this.page = page ;
		this.page.setBaseAction(this);
		this.utils = page.getUtils() ;
		this.form = this.utils.getForm();
		this.ret = this.utils.getReturnData();
		prepareActionEnv();
	}
	
	
	/**
	 * @throws Exception
	 * 准备action的环境变量
	 */
	protected void prepareActionEnv() throws Exception{
		
	}
	
	protected void validatePrivilege(){
		String uri = page.getRequest().getRequestURI();
		if(!page.IsBackEndMode() && uri.startsWith("/jmPro/subPro/") && StringHelper.IsNullOrEmpty(form.getAction())){
			uri = uri.replaceAll("/jmPro", "");
			if(uri.replaceAll("/subPro/", "").indexOf("/") >= 0 && !uri.startsWith("/subPro/admin/") && !uri.startsWith("/subPro/public/")){
				//驗證subPro的頁面權限
				String panmenusn = StringHelper.getValue(this.getRequest().getParameter("panmenusn"));
				if(StringHelper.IsNullOrEmpty(panmenusn) && !page.isMenuPage()){
					return;
				}
				
				if(StringHelper.IsNullOrEmpty(panmenusn)){
					page.setPrivilege(false);return;
				}
				
				StringBuilderEx tempsql = new StringBuilderEx();
				tempsql.Reset();
				tempsql.Append("select src from pan.dbo.PAN_SUBPRO_MENU where  sn =%1$s ",panmenusn);
				tempsql.Append("and (");
				tempsql.Append(" [public] = 1");
				tempsql.Append(" or charindex('%1$s',(','+hrnums+',')) > 0",StringHelper.getValue(utils.getSession().getAttribute("sub_hrnum"), utils.getHrNum()));
				tempsql.Append(" or charindex('%1$s',(','+hr003tsns+',')) > 0",StringHelper.getValue(utils.getSession().getAttribute("sub_hr003tsn"), utils.getHr003Tsn()));
				tempsql.Append(" or charindex('%1$s',(','+ips+',')) > 0",utils.getRemoteAddr());
				tempsql.Append(")");
				try{
					ResultSet temprs = utils.executeQuery("info5",tempsql.toString());
					if(!temprs.next()){
						page.setPrivilege(false);return;
					}
					String src = utils.getRsString(temprs, "src");
					if(StringHelper.IsNullOrEmpty(src)){
						page.setPrivilege(false);return;
					}
					src = src.substring(0,src.indexOf(".jsp")+4);
					if(!temprs.next() && !uri.equals(src)){
						page.setPrivilege(false);
					}
				}catch(Exception e){
					page.setPrivilege(false);return;
				}
			}
		}
	}
	
	protected boolean isValidateReq(){
		if(page.IsBackEndMode() || this.getRequest().getRemoteAddr().equals("127.0.0.1") )
			return true;
		if(StringHelper.IsNullOrEmpty(form.getToken())){
			return true;
		}
		
		if(StringHelper.Compare("search",form.getAction(),true) == 0
				||StringHelper.Compare("export",form.getAction(),true) == 0
				||page.isExport()
				||page.isDebug()
		){
			return true;
		}
		
		return Token.isTokenStringValid(form.getToken(),this.getRequest().getSession());
	}
	
	public void Process() throws Exception{
		utils.bindForm();
		
		if(isValidateReq()){
			validatePrivilege();
			if(this.getPage() instanceof WFPage && page.isPrivilege()){
				BeginWFProcess();
			}
			
			if(page.isPrivilege()){
				try{
					OnProcess(form.getAction());
				}catch (Exception e) {
					e.printStackTrace();
					throw e;
				}
			}
		}else{
			ret.setErrorInfo(utils.i18n("請勿重複提交")+"!");
			ret.AppendJSCode(StringHelper.Format("alert('%1$s!');",utils.i18n("請勿重複提交")));
		}
		
		if(page.isPrivilege()){EndProcess();}
		
		if(this.getPage() instanceof WFPage && page.isPrivilege()){
			EndWFProcess();
		}
		
		if(!this.getPage().IsBackEndMode())
			this.getPage().forwardUIPage();
	}
	
	//請求處理結束
	protected void EndProcess() throws Exception{
		
	}
	
	protected boolean OnProcess(String action) throws Exception{
		if(StringHelper.IsNullOrEmpty(action)){
			return onLoadDefault(); 
		}
		
		if("new".equalsIgnoreCase(action)){
			return onNew();
		}
		if("search".equalsIgnoreCase(action)){
			return onSearch();
		}else if("save".equalsIgnoreCase(action)){
			return onSave();
		}else if("cancel".equalsIgnoreCase(action)){
			return onCancel();	
		}else if("delete".equalsIgnoreCase(action)){
			return onDelete();
		}else if("copy".equalsIgnoreCase(action)){
			return onCopy();			
		}else if("start".equalsIgnoreCase(action)){
			return onStart();
		}else if("check".equalsIgnoreCase(action)){
			return onCheck();			
		}else if("export".equalsIgnoreCase(action)){
			return onExport();
		}else if("print".equalsIgnoreCase(action)){
			return onPrint();			
		}else if("loading".equalsIgnoreCase(action)){
			return onLoading();			
		}else if("loadingCancel".equalsIgnoreCase(action)){
			return onLoadingCancel();			
		}else if("loadingSave".equalsIgnoreCase(action)){
			return onLoadingSave();			
		}else if("wfcheck".equalsIgnoreCase(action)){
			return onWFCheck();			
		}else if("wfstart".equalsIgnoreCase(action)){
			return onWFStart();			
		}else if("wfcancel".equalsIgnoreCase(action)){
			return onWFCancel();			
		}else {
			return onCustomAction(action);			
		}
	}
	
	/**
	 * 加載頁面默認數據
	 */
	protected boolean onLoadDefault()  throws Exception{
		return true ;
	}
	
	/**
	 * 处理新增
	 */
	protected boolean onNew()  throws Exception{
		onNewValidate();
		if(ret.isError())return false;
		doNew();
		return true ;
	}
	/**
	 * 列印验证
	 */
	protected void onNewValidate() throws Exception{}
	/**
	 * 执行列印
	 */
	protected void doNew() throws Exception{}
	
	
	/**
	 * 处理查询
	 */
	protected boolean onSearch()  throws Exception{
		onSearchValidate();
		if(ret.isError())return false;
		onSearchRetrieveResult();
		initFormControls();
		return true ;
	}
	
	/**
	 * 处理查询的驗證
	 */
	protected void onSearchValidate() throws Exception{}
	
	/**
	 * 处理查询的驗證
	 */
	protected void onSearchRetrieveResult() throws Exception{}
	
	/**
	 * 初始化表單控件
	 */
	protected void initFormControls() throws Exception{}
	
	/**
	 * 取出查詢語句
	 */
	protected String onSearchGetSql() throws Exception{return "";}
	
	
	/**
	 * 处理存储
	 */
	protected boolean onSave()  throws Exception{
		onSaveValidate();
		if(ret.isError())return false;
		doSave();
		return true ;
	}
	/**
	 * 存储验证
	 */
	protected void onSaveValidate() throws Exception{}
	/**
	 * 执行存储
	 */
	protected void doSave() throws Exception{}
	
	/**
	 * 处理取消
	 */
	protected boolean onCancel()  throws Exception{
		onCancelValidate();
		if(ret.isError())return false;
		doCancel();
		return true ;
	}
	/**
	 * 取消验证
	 */
	protected void onCancelValidate() throws Exception{}
	/**
	 * 取消删除
	 */
	protected void doCancel() throws Exception{}
	
	/**
	 * 处理删除
	 */
	protected boolean onDelete()  throws Exception{
		onDeleteValidate();
		if(ret.isError())return false;
		doDelete();
		return true ;
	}
	/**
	 * 删除验证
	 */
	protected void onDeleteValidate() throws Exception{}
	/**
	 * 执行删除
	 */
	protected void doDelete() throws Exception{}
	
	
	
	/**
	 * 处理复制
	 */
	protected boolean onCopy()  throws Exception{
		onCopyValidate();
		if(ret.isError())return false;
		doCopy();
		return true ;
	}
	/**
	 * 删除验证
	 */
	protected void onCopyValidate() throws Exception{}
	/**
	 * 执行复制
	 */
	protected void doCopy() throws Exception{}
	
	
	/**
	 * 处理送签
	 */
	protected boolean onStart()  throws Exception{
		onStartValidate();
		if(ret.isError())return false;
		doStart();
		return true ;
	}
	/**
	 * 送签验证
	 */
	protected void onStartValidate() throws Exception{}
	/**
	 * 执行送签
	 */
	protected void doStart() throws Exception{}
	
	
	/**
	 * 处理签核
	 */
	protected boolean onCheck()  throws Exception{
		onCheckValidate();
		if(ret.isError())return false;
		doCheck();
		return true ;
	}
	/**
	 * 签核验证
	 */
	protected void onCheckValidate() throws Exception{}
	/**
	 * 执行签核
	 */
	protected void doCheck() throws Exception{}
	
	
	/**
	 * 处理导出
	 */
	protected boolean onExport()  throws Exception{
		onExportValidate();
		if(ret.isError())return false;
		doExport();
		return true ;
	}
	/**
	 * 导出验证
	 */
	protected void onExportValidate() throws Exception{}
	/**
	 * 执行导出
	 */
	protected void doExport() throws Exception{
		this.getPage().setExportType(StringHelper.getValue(form.getActionValue(),"xls"));
		
		if(StringHelper.IsNullOrEmpty(form.getOldAction())){
			OnProcess("");
		}else  if(StringHelper.Compare("search",form.getOldAction(),true)==0){
			form.SetParamValue("action",form.getOldAction());
			form.SetParamValue("actionvalue",form.getOldActionValue());
			form.setAction(form.getOldAction());
			form.setActionValue(form.getOldActionValue());
			OnProcess(form.getOldAction());
		}else{
			ret.setErrorInfo(utils.i18n("請先執行查詢,然後再導出資料!"));
		}
		return ;
	}
	
	
	/**
	 * 处理列印
	 */
	protected boolean onPrint()  throws Exception{
		onPrintValidate();
		if(ret.isError())return false;
		doPrint();
		return true ;
	}
	/**
	 * 列印验证
	 */
	protected void onPrintValidate() throws Exception{}
	/**
	 * 执行列印
	 */
	protected void doPrint() throws Exception{}
	
	/**
	 * 处理Loading登陆
	 */
	protected boolean onLoading()  throws Exception{
		synchronized(this.getRequest().getSession()){
			PageResult pageResult = ret.getCallResult();
			pageResult.getData().SetParamValue("index",form.gpiv("index",0));
			ret.setOkInfo("ok");
			
			onLoadingValidate();
			if(!ret.isError())
				doLoading();
			
			if(ret.isError()){
				pageResult.getData().SetParamValue("status",ret.getCallResult().getErrorInfo());
			}else{
				pageResult.getData().SetParamValue("status",ret.getCallResult().getOkInfo());
			}
			
			this.getPage().Output(pageResult.ToJSONString());
			return true ;
		}
	}
	/**
	 * Loading登陆验证
	 */
	protected void onLoadingValidate() throws Exception{}
	/**
	 * 执行Loading登陆
	 */
	protected void doLoading() throws Exception{}
	
	/**
	 * 处理Loading取消
	 */
	protected boolean onLoadingCancel()  throws Exception{
		PageResult pageResult = ret.getCallResult();
		pageResult.getData().SetParamValue("index",form.gpiv("index",0));
		ret.setOkInfo("已取消!");
		
		onLoadingCancelValidate();
		if(!ret.isError())
			doLoadingCancel();
		
		if(ret.isError()){
			pageResult.getData().SetParamValue("status",ret.getCallResult().getErrorInfo());
		}else{
			pageResult.getData().SetParamValue("status",ret.getCallResult().getOkInfo());
		}
		
		this.getPage().Output(pageResult.ToJSONString());
		return true ;
	}
	
	/**
	 * Loading取消验证
	 */
	protected void onLoadingCancelValidate() throws Exception{}
	/**
	 * 执行Loading取消
	 */
	protected void doLoadingCancel() throws Exception{}
	
	/**
	 * 处理Loading保存
	 */
	protected boolean onLoadingSave()  throws Exception{
		PageResult pageResult = ret.getCallResult();
		pageResult.getData().SetParamValue("index",form.gpiv("index",0));
		ret.setOkInfo("保存成功!");
		
		onLoadingSaveValidate();
		if(!ret.isError())
			doLoadingSave();
		
		if(ret.isError()){
			pageResult.getData().SetParamValue("status",ret.getCallResult().getErrorInfo());
		}else{
			pageResult.getData().SetParamValue("status",ret.getCallResult().getOkInfo());
		}
		
		this.getPage().Output(pageResult.ToJSONString());
		return true ;
	}
	
	/**
	 * Loading保存验证
	 */
	protected void onLoadingSaveValidate() throws Exception{}
	/**
	 * 执行Loaing保存
	 */
	protected void doLoadingSave() throws Exception{}
	
	
	/**
	 * 处理自定义动作
	 */
	protected boolean onCustomAction(String action) throws Exception{
		return true ;
	}
	
	public HttpServletRequest getRequest(){
		return this.getPage().getRequest();
	} 
	
	public HttpServletResponse getResponse(){
		return this.getPage().getResponse();
	} 
	
	public Utils getUtils(){
		return this.utils;
	}
	
	public BaseMainPage getPage(){
		return this.page;
	}
	
	public ReturnData getRet() {
		return ret;
	}
}
